/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.integral.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medusa.gruul.account.api.entity.MemberCardPrivilege;
import com.medusa.gruul.account.api.feign.RemoteMiniAccountService;
import com.medusa.gruul.account.api.model.AccountInfoDto;
import com.medusa.gruul.account.api.model.IntegraChangelDto;
import com.medusa.gruul.account.api.model.MemberCardInfoCenterDto;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.common.core.util.SystemCode;
import com.medusa.gruul.common.data.tenant.ShopContextHolder;
import com.medusa.gruul.common.data.tenant.TenantContextHolder;
import com.medusa.gruul.integral.api.constant.IntegralConstant;
import com.medusa.gruul.integral.api.entity.IntegralDetail;
import com.medusa.gruul.integral.api.entity.IntegralRule;
import com.medusa.gruul.integral.api.entity.IntegralRuleParameter;
import com.medusa.gruul.integral.api.entity.IntegralShop;
import com.medusa.gruul.integral.api.model.IntegralCouponDto;
import com.medusa.gruul.integral.mapper.IntegralDetailMapper;
import com.medusa.gruul.integral.mapper.IntegralRuleMapper;
import com.medusa.gruul.integral.mapper.IntegralRuleParameterMapper;
import com.medusa.gruul.integral.mapper.IntegralShopMapper;
import com.medusa.gruul.integral.mq.Sender;
import com.medusa.gruul.integral.service.IIntegralDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medusa.gruul.integral.web.enums.IntegralGainTypeEnum;
import com.medusa.gruul.integral.web.enums.IntegralOperationTypeEnum;
import com.medusa.gruul.integral.web.model.dto.IntegralDetailDto;
import com.medusa.gruul.integral.web.model.param.api.IntegralDetailParam;
import com.medusa.gruul.integral.web.model.vo.IntegralDetailVo;
import com.medusa.gruul.integral.api.model.UserInfoVo;
import com.medusa.gruul.order.api.entity.OrderItem;
import com.medusa.gruul.order.api.model.OrderFailMessage;
import com.medusa.gruul.order.api.model.OrderItemVo;
import com.medusa.gruul.order.api.model.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 积分明细表
 服务实现类
 * </p>
 *
 * @author xiaoq
 * @since 2019-11-19
 */
@Service
@Slf4j
public class IntegralDetailServiceImpl extends ServiceImpl<IntegralDetailMapper, IntegralDetail>
		implements IIntegralDetailService {
	@Autowired
	private IntegralDetailMapper integralDetailMapper;
	@Autowired
	private IntegralRuleMapper integralRuleMapper;
	@Autowired
	private IntegralRuleParameterMapper integralRuleParameterMapper;
	@Autowired
	private IntegralShopMapper integralShopMapper;

	@Autowired
	private RemoteMiniAccountService remoteMiniAccountService;

	@Autowired
	private Sender sender;


	/**
	 * 用户查询积分明细
	 *
	 * @param integralDetailParam 分页对象
	 * @return 明细记录
	 */
	@Override
	public IPage<IntegralDetailVo> selectIntegralDetail(IntegralDetailParam integralDetailParam) {
		Page<IntegralDetailVo> page = new Page<>(integralDetailParam.getCurrent(), integralDetailParam.getSize());
		return page.setRecords(this.baseMapper.getIntegralDetail(page, integralDetailParam));
	}


	/**
	 * 删除积分明细记录
	 *
	 * @param integralDetailId 积分明细Id
	 */
	@Override
	public void delDetail(Long integralDetailId) {
		Integer integralDetail = integralDetailMapper.deleteById(integralDetailId);
		if (integralDetail < 1) {
			throw new ServiceException("积分明细记录删除失败", SystemCode.DATA_DELETE_FAILED.getCode());
		}
	}


	/**
	 * 用户每日首次分享 得到该店铺设置的分享积分值
	 * @param shopId 商铺id
	 * @param userId 用户Id
	 * @return 用户分享获得的积分值
	 */
	@Override
	public BigDecimal addIntegralDetail(String shopId, String userId) {
		String tenantId = TenantContextHolder.getTenantId();
		//1.根据店铺id 得到积分商城表信息
		BigDecimal integral = null;
		IntegralShop integralShop = integralShopMapper.selectByShopInfo(shopId);
		if (integralShop == null) {
			throw new ServiceException("商城信息不存在", SystemCode.DATA_NOT_EXIST.getCode());
		}
		//积分商城是否开启
		if (integralShop.getType()) {
			//2.根据积分商城表id 得到积分规则表类型为分享的 数据
			IntegralRule integralRule = integralRuleMapper.selectIntegralRuleInfo(integralShop.getId());
			if (integralRule == null) {
				throw new ServiceException("分享数据信息不存在", SystemCode.DATA_NOT_EXIST.getCode());
			}
			//查看商家是否开启分享功能
			if (integralRule.getOpen()) {
				//3.根据积分规则表分享信息得到 商家设置的分享得到积分
				IntegralRuleParameter shareIntegral = integralRuleParameterMapper
						.getShareIntegral(integralRule.getIntegralRuleId());
				//4.查询积分明细 用户今日是否有进行分享得到积分
				List<IntegralDetail> integralDetails = integralDetailMapper.selectDetailInfo(shopId, userId);
				//5.判断用户今日是否为首次分享
				if (integralDetails.size() > 0) {
					return null;
				}
				//获取用户会员倍数 非会员或者无积分权益 倍数为1
				BigDecimal integralMultiples = memberCardIntegralPrivilegeInfo(tenantId, userId);
				IntegralDetail integralDetail = new IntegralDetail();
				if (integralMultiples.compareTo(BigDecimal.ONE) == 1) {
					//拥有会员权益 积分加倍时 新增字段 会员权益
					integralDetail
							.setDetailName(integralMultiples.doubleValue() + IntegralConstant.INTEGRAL_PRIVILEGE_NAME);
				}
				integralDetail.setGainIntegralType(IntegralGainTypeEnum.DAY_SHARE.getStatus());
				integralDetail.setUserId(userId);
				integralDetail.setIntegral(shareIntegral.getValue().multiply(integralMultiples));
				//计算用户现在拥有的积分值(未加当前获取的积分)
				BigDecimal bigDecimal = integralDetailMapper.selectNowIntegral(shopId, userId);
				//本次获取的积分
				integral = integralDetail.getIntegral();
				bigDecimal = bigDecimal == null ? new BigDecimal(0) : bigDecimal;
				//用户目前积分(现有积分+本次获得的积分)
				integralDetail.setNowTheIntegral(bigDecimal.add(integral));
				int insert = integralDetailMapper.insert(integralDetail);
				if (insert < 1) {
					throw new ServiceException("积分明细生成失败", SystemCode.DATA_ADD_FAILED.getCode());
				}
				//mq通知用户

				IntegraChangelDto integraChangelDto = new IntegraChangelDto();
				integraChangelDto.setValue(integral);
				integraChangelDto.setUserId(userId);
				integraChangelDto.setOption(IntegralOperationTypeEnum.INCREASE.getStatus());
				integraChangelDto.setTenantId(tenantId);
				sender.sendIntegralDeductionMessage(integraChangelDto);
			}
		}
		return integral;
	}

	/**
	 * 生成积分归还 积分明细
	 *
	 * @param userId 用户Id
	 * @param tenantId 租户Id
	 * @param integralValue 积分值
	 * @return 插入积分明细 归还积分是否成功
	 */
	@Override
	public Boolean addIntegralDetailRecord(String userId, String tenantId, BigDecimal integralValue) {
		String shopId = ShopContextHolder.getShopId();
		IntegralDetail integralDetail = new IntegralDetail();
		integralDetail.setIntegral(integralValue);
		integralDetail.setGainIntegralType(IntegralGainTypeEnum.GIVE_BACK.getStatus());
		integralDetail.setUserId(userId);
		BigDecimal bigDecimal = integralDetailMapper.selectNowIntegral(shopId, userId);
		bigDecimal = bigDecimal == null ? new BigDecimal(0) : bigDecimal;
		integralDetail.setNowTheIntegral(bigDecimal.add(integralValue));
		log.info("归还积分为        " + integralValue);
		boolean flag = Boolean.TRUE;
		int insert = integralDetailMapper.insert(integralDetail);
		//判断是否插入成功
		if (insert < 1) {
			flag = Boolean.FALSE;
		}
		return flag;
	}


	/**
	 * 生成积分扣减 积分明细
	 *
	 * @param userId 用戶id
	 * @param tenantId 租戶id
	 * @param integralValue  积分值
	 */
	@Override
	public Boolean subtractIntegralDetailRecord(String userId, String tenantId, BigDecimal integralValue) {
		String shopId = ShopContextHolder.getShopId();
		IntegralDetail integralDetail = new IntegralDetail();
		integralDetail.setIntegral(integralValue.negate());
		integralDetail.setGainIntegralType(IntegralGainTypeEnum.USE_BUY_GOODS.getStatus());
		integralDetail.setUserId(userId);
		System.out.println("本次扣减积分为            " + integralValue);
		BigDecimal bigDecimal = integralDetailMapper.selectNowIntegral(shopId, userId);
		bigDecimal = bigDecimal == null ? new BigDecimal(0) : bigDecimal;
		integralDetail.setNowTheIntegral(bigDecimal.add(integralValue.negate()));
		int insert = integralDetailMapper.insert(integralDetail);
		//判断是否插入成功
		boolean flag = Boolean.TRUE;
		if (insert < 1) {
			flag = Boolean.FALSE;
		}
		return flag;
	}


	/**
	 * 生成兑换积分优惠券信息
	 * @param integralCouponDto
	 * @return
	 */
	@Override
	public Boolean addConversionIntegralDetail(IntegralCouponDto integralCouponDto) {
		IntegralDetail integralDetail = new IntegralDetail();
		integralDetail.setIntegral(integralCouponDto.getIntegralValue().negate());
		integralDetail.setGainIntegralType(IntegralGainTypeEnum.EXCHANGE_INTEGRAL_GOODS.getStatus());
		integralDetail.setUserId(integralCouponDto.getUserId());
		BigDecimal bigDecimal = integralDetailMapper
				.selectNowIntegral(integralDetail.getShopId(), integralCouponDto.getUserId());
		bigDecimal = bigDecimal == null ? new BigDecimal(0) : bigDecimal;
		integralDetail.setNowTheIntegral(bigDecimal.add(integralCouponDto.getIntegralValue().negate()));
		int insert = integralDetailMapper.insert(integralDetail);
		//判断是否插入成功
		boolean flag = Boolean.TRUE;
		if (insert < 1) {
			flag = Boolean.FALSE;
		}
		return flag;
	}


	/**
	 * 商家充值积分 生成明细记录 发mq给用户
	 *
	 * @param userIds 用户ids
	 * @param integral 积分值
	 */
	@Override
	public void rechargeIntegral(List<String> userIds, BigDecimal integral) {
		//遍历userIds 逐一插入明细 修改数据
		for (String userId : userIds) {
			String shopId = ShopContextHolder.getShopId();
			List<Integer> list = new ArrayList<Integer>(2);
			list.add(2);
			AccountInfoDto accountInfoDto = remoteMiniAccountService.accountInfo(userId, list);
			if (accountInfoDto == null) {
				throw new ServiceException("用户不存在", SystemCode.DATA_NOT_EXIST.getCode());
			}
			IntegralDetail integralDetail = new IntegralDetail();
			integralDetail.setIntegral(integral);
			integralDetail.setGainIntegralType(IntegralGainTypeEnum.MERCHANT_RECHARGE.getStatus());
			integralDetail.setUserId(userId);
			BigDecimal bigDecimal = integralDetailMapper.selectNowIntegral(shopId, userId);
			bigDecimal = bigDecimal == null ? new BigDecimal(0) : bigDecimal;
			integralDetail.setNowTheIntegral(bigDecimal.add(integral));
			int insert = integralDetailMapper.insert(integralDetail);
			if (insert < 1) {
				throw new ServiceException("积分明细生成失败", SystemCode.DATA_ADD_FAILED.getCode());
			}
			//mq通知用户
			String tenantId = TenantContextHolder.getTenantId();
			IntegraChangelDto integraChangelDto = new IntegraChangelDto();
			integraChangelDto.setValue(integral);
			integraChangelDto.setUserId(userId);
			integraChangelDto.setOption(IntegralOperationTypeEnum.INCREASE.getStatus());
			integraChangelDto.setTenantId(tenantId);
			sender.sendIntegralDeductionMessage(integraChangelDto);
		}
	}


	/**
	 * 商家扣除积分 生成明细记录 发mq给用户
	 *
	 * @param userIds 用户ids
	 * @param integral 积分值
	 */
	@Override
	public void deductIntegral(List<String> userIds, BigDecimal integral) {
		//遍历userIds 逐一插入明细 修改数据
		for (String userId : userIds) {
			String shopId = ShopContextHolder.getShopId();
			List<Integer> list = new ArrayList<Integer>(2);
			list.add(2);
			AccountInfoDto accountInfoDto = remoteMiniAccountService.accountInfo(userId, list);
			if (accountInfoDto.getMiniAccountExtends() == null) {
				throw new ServiceException("用户不存在", SystemCode.DATA_NOT_EXIST.getCode());
			}
			if (accountInfoDto.getMiniAccountExtends().getIntegral().compareTo(integral) == -1) {
				throw new ServiceException("用户积分不足");
			}
			IntegralDetail integralDetail = new IntegralDetail();
			integralDetail.setIntegral(integral.negate());
			integralDetail.setGainIntegralType(IntegralGainTypeEnum.MERCHANT_DEDUCT.getStatus());
			integralDetail.setUserId(userId);
			BigDecimal bigDecimal = integralDetailMapper.selectNowIntegral(shopId, userId);
			bigDecimal = bigDecimal == null ? new BigDecimal(0) : bigDecimal;
			integralDetail.setNowTheIntegral(bigDecimal.add(integral.negate()));
			int insert = integralDetailMapper.insert(integralDetail);
			if (insert < 1) {
				throw new ServiceException("积分明细生成失败", SystemCode.DATA_ADD_FAILED.getCode());
			}
			//mq通知用户
			String tenantId = TenantContextHolder.getTenantId();
			IntegraChangelDto integraChangelDto = new IntegraChangelDto();
			integraChangelDto.setValue(integral);
			integraChangelDto.setUserId(userId);
			integraChangelDto.setOption(IntegralOperationTypeEnum.REDUCE.getStatus());
			integraChangelDto.setTenantId(tenantId);
			sender.sendIntegralDeductionMessage(integraChangelDto);
		}
	}


	/**
	 * 获取用户信息 积分值 头像 昵称
	 * @param userId 用户id
	 * @return 用户信息
	 */
	@Override
	public UserInfoVo getUserInfo(String userId) {
		List<Integer> list = new ArrayList<Integer>(3);
		list.add(1);
		list.add(2);
		UserInfoVo userInfoVo = new UserInfoVo();
		AccountInfoDto accountInfoDto = remoteMiniAccountService.accountInfo(userId.toString(), list);
		if (accountInfoDto.getMiniAccountExtends() == null) {
			throw new ServiceException("用户不存在", SystemCode.DATA_NOT_EXIST.getCode());
		}
		userInfoVo.setIntegral(accountInfoDto.getMiniAccountExtends().getIntegral());
		userInfoVo.setAvatarUrl(accountInfoDto.getMiniAccountunt().getAvatarUrl());
		userInfoVo.setNickName(accountInfoDto.getMiniAccountunt().getNikeName());
		return userInfoVo;
	}


	/**
	 * 订阅已完成的订单 生成明细 通知用户
	 *
	 * @param orderVo 订单详情
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void orderFinish(OrderVo orderVo) {
		//生成积分明细记录
		IntegralDetail integralDetail = new IntegralDetail();
		integralDetail.setShopId(orderVo.getShopId());
		integralDetail.setTenantId(orderVo.getTenantId());
		integralDetail.setUserId(orderVo.getUserId());
		integralDetail.setGainIntegralType(IntegralGainTypeEnum.BUY_GOODS.getStatus());
		List<OrderItemVo> orderItemList = orderVo.getOrderItemList();
		if (orderItemList.size() > 0) {
			//遍历订单详情
			for (OrderItemVo orderItem : orderItemList) {
				if (orderItem.getGiftIntegration().compareTo(BigDecimal.ZERO) == 0) {
					//商品赠送积分为0时 不生成积分明细
					break;
				}
				integralDetail.setQuantity(orderItem.getProductQuantity());
				//所获得积分值   =    赠送的积分   *   购买数量
                BigDecimal integralValue = orderItem.getGiftIntegration();
                log.info("打印订单数据：赠送积分"+orderItem.getGiftIntegration());
                log.info("打印订单数据：购买数量"+orderItem.getProductQuantity());
				//判断用户是否拥有会员卡
				BigDecimal integralMultiples = memberCardIntegralPrivilegeInfo(orderVo.getTenantId(),
						orderVo.getUserId());
				if (integralMultiples.compareTo(BigDecimal.ONE) == 1) {
					System.out.println(integralMultiples + "," + BigDecimal.ONE);
					//拥有会员权益 积分加倍时 新增字段 会员权益
					integralDetail
							.setDetailName(integralMultiples.doubleValue() + IntegralConstant.INTEGRAL_PRIVILEGE_NAME);
				}
				integralDetail.setIntegral(integralValue);
				BigDecimal bigDecimal = integralDetailMapper
						.selectNowIntegral(integralDetail.getShopId(), orderVo.getUserId());
				if (bigDecimal == null) {
					bigDecimal = new BigDecimal(0);
				}
				integralDetail.setNowTheIntegral(bigDecimal.add(integralValue));

				int insert = integralDetailMapper.insert(integralDetail);
				if (insert < 1) {
					throw new ServiceException("积分明细插入失败", SystemCode.DATA_ADD_FAILED.getCode());
				}
				//通知用户
				IntegraChangelDto integraChangelDto = new IntegraChangelDto();
				integraChangelDto.setValue(integralValue);
				integraChangelDto.setUserId(orderVo.getUserId());
				integraChangelDto.setOption(IntegralOperationTypeEnum.INCREASE.getStatus());
				integraChangelDto.setTenantId(orderVo.getTenantId());
				sender.sendIntegralDeductionMessage(integraChangelDto);
			}
		}
	}


	/**
	 * 获取需要清空的积分 (根据积分清空时间计算 获取积分时间小于createTime的积分明细数据)
	 *
	 * @param shopId 商铺id
	 * @param tenantId 租户id
	 * @param createTime 积分清空时间 (对积分获取时间小于该时间的积分进行清空)
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getNeedClearIntegrals(String shopId, String tenantId, String createTime) {
		return integralDetailMapper.selectNeedClearIntegrals(shopId, tenantId, createTime);
	}


	/**
	 * 获取需要清空的用户以及积分值
	 *
	 * @param shopId 商品id
	 * @param tenantId 租户id
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectAllUser(String shopId, String tenantId) {
		return integralDetailMapper.getAllUser(shopId, tenantId);
	}


	/**
	 * 生成新的积分明细 并通知用户扣积分
	 *
	 * @param userId 用户id
	 * @param integral 扣除积分值
	 * @param shopId 商品id
	 * @param tenantId 租户id
	 */
	@Override
	public void generateNowIntegralDetailService(String userId, BigDecimal integral, String shopId, String tenantId) {
		//生成积分明细
		IntegralDetail integralDetail = new IntegralDetail();
		integralDetail.setClear(true);
		integralDetail.setGainIntegralType(IntegralGainTypeEnum.EMPTY.getStatus());
		integralDetail.setNowTheIntegral(BigDecimal.ZERO);
		integralDetail.setTenantId(tenantId);
		integralDetail.setShopId(shopId);
		integralDetail.setUserId(userId);
		integralDetail.setIntegral(integral.negate());
		int insert = integralDetailMapper.insert(integralDetail);
		if (insert < 1) {
			throw new ServiceException("积分清空明细插入失败", SystemCode.DATA_ADD_FAILED.getCode());
		}
		//2.1通知用户扣积分
		IntegraChangelDto integraChangelDto = new IntegraChangelDto();
		integraChangelDto.setValue(integral);
		integraChangelDto.setUserId(userId);
		integraChangelDto.setOption(IntegralOperationTypeEnum.REDUCE.getStatus());
		integraChangelDto.setTenantId(tenantId);
		sender.sendIntegralDeductionMessage(integraChangelDto);
	}

	/**
	 * 修改这个商铺中所有的积分明细信息为 已清空状态
	 *
	 * @param shopId 商铺id
	 * @param tenantId 租户id
	 */
	@Override
	public void updateAll(String shopId, String tenantId) {
		integralDetailMapper.updateAllStatus(shopId, tenantId);
	}


	/**
	 * 获取积分明细 (pc端)
	 * @param integralDetailDto 查询条件
	 * @return 符合条件的数据
	 */
	@Override
	public IPage<IntegralDetailVo> getIntegralDetail(IntegralDetailDto integralDetailDto) {
		Page<IntegralDetailVo> page = new Page<>(integralDetailDto.getCurrent(), integralDetailDto.getSize());
		return page.setRecords(this.baseMapper.selectIntegralDetail(page, integralDetailDto));
	}

	/**
	 * 创建订单失败  归还用户积分
	 * @param orderFailMessage
	 */
	@Override
	public void createOrderFail(OrderFailMessage orderFailMessage) {
		//生成积分明细
		if (orderFailMessage.getIntegration().compareTo(BigDecimal.ZERO) == 0) {
			return;
		}
		IntegralDetail integralDetail = new IntegralDetail();
		integralDetail.setShopId(orderFailMessage.getShopId());
		integralDetail.setTenantId(orderFailMessage.getTenantId());
		integralDetail.setUserId(orderFailMessage.getUserId());
		integralDetail.setIntegral(orderFailMessage.getIntegration());
		integralDetail.setGainIntegralType(IntegralGainTypeEnum.GIVE_BACK.getStatus());
		BigDecimal bigDecimal = integralDetailMapper
				.selectNowIntegral(integralDetail.getShopId(), orderFailMessage.getUserId());
		if (bigDecimal == null) {
			bigDecimal = new BigDecimal(0);
		}
		integralDetail.setNowTheIntegral(bigDecimal.add(orderFailMessage.getIntegration()));
		int insert = integralDetailMapper.insert(integralDetail);
		if (insert < 1) {
			throw new ServiceException("积分归还明细插入失败", SystemCode.DATA_ADD_FAILED.getCode());
		}
		IntegraChangelDto integraChangelDto = new IntegraChangelDto();
		integraChangelDto.setValue(orderFailMessage.getIntegration());
		integraChangelDto.setUserId(orderFailMessage.getUserId());
		integraChangelDto.setOption(IntegralOperationTypeEnum.INCREASE.getStatus());
		integraChangelDto.setTenantId(orderFailMessage.getTenantId());
		sender.sendIntegralDeductionMessage(integraChangelDto);

	}

    /**
     * 使用积分兑换会员卡
     * @param userId 用户id
     * @param integralValue 所需积分值
     * @return 状态码
     */
	@Override
	public Result useIntegralConversionClubCard(String userId, BigDecimal integralValue) {
		String tenantId = TenantContextHolder.getTenantId();
		String shopId = ShopContextHolder.getShopId();
		AccountInfoDto accountInfoDto = remoteMiniAccountService.accountInfo(userId, Arrays.asList(2));
		//得到用户总积分值
		BigDecimal integral = accountInfoDto.getMiniAccountExtends().getIntegral();
			if (integral.compareTo(integralValue) > -1) {
				IntegralDetail integralDetail = new IntegralDetail();
				integralDetail.setIntegral(integralValue.negate());
				integralDetail.setGainIntegralType(IntegralGainTypeEnum.CONVERSION_CLUB_CARD.getStatus());
				integralDetail.setUserId(userId);
				BigDecimal bigDecimal = integralDetailMapper.selectNowIntegral(shopId, userId);
				if (bigDecimal == null) {
					bigDecimal = new BigDecimal(0);
				}
				integralDetail.setNowTheIntegral(bigDecimal.add(integralValue.negate()));
				int insert = integralDetailMapper.insert(integralDetail);
				if (insert < 1) {
					throw new ServiceException("积分明細生成失败", SystemCode.DATA_ADD_FAILED.getCode());
				}
				//通知用户扣除积分
				IntegraChangelDto integraChangelDto = new IntegraChangelDto();
				integraChangelDto.setValue(integralValue);
				integraChangelDto.setUserId(userId);
				integraChangelDto.setOption(IntegralOperationTypeEnum.REDUCE.getStatus());
				integraChangelDto.setTenantId(tenantId);
				sender.sendIntegralDeductionMessage(integraChangelDto);
				return Result.ok(SystemCode.SUCCESS.getCode());
			}
			return Result.failed(SystemCode.FAILURE.getCode(), "积分不足");
	}


	/**
	 *  获取当前用户的会员卡积分权益信息
	 *
	 * @param tenantId 租户id
	 * @param userId 用户id
	 * @return 返回会员积分权益倍数
	 */
	protected BigDecimal memberCardIntegralPrivilegeInfo(String tenantId, String userId) {
		//查看用户会员信息
		MemberCardInfoCenterDto memberInfo = remoteMiniAccountService.getMemberInfo(tenantId, userId);
		log.info(tenantId + "," + userId);
		//判断该用户是否是会员 即为是否存在会员卡
		System.out.println(memberInfo + "用户会员信息");
		if (ObjectUtil.isNotNull(memberInfo)) {
			System.out.println("拥有会员卡");
			List<MemberCardPrivilege> memberCardPrivileges = memberInfo.getMemberCardPrivileges();
			//是否存在会员权益
			if (CollUtil.isNotEmpty(memberInfo.getMemberCardPrivileges())) {
				for (MemberCardPrivilege memberCardPrivilege : memberCardPrivileges) {
					if (IntegralConstant.INTEGRAL_PRIVILEGE_TYPE.equals(memberCardPrivilege.getPType())
							& memberCardPrivilege.getOpen() == 1) {
						log.info("拥有会员权益");
						return memberCardPrivilege.getDefValue();
					}
				}
			}
		}
		return BigDecimal.ONE;
	}


	//-----------------------------------------------废弃方法-----------------------------------
	/**
	 *
	 *  该实现类未使用
	 * 生成积分清空明细 并通知用户
	 * @param needClearIntegral
	 * @param shopId
	 * @param tenantId
	 */

	/**
	 @Override public void clearIntegral(Map<String, Object> needClearIntegral, String shopId, String tenantId) {
	 //生成积分明细记录
	 IntegralDetail integralDetail = new IntegralDetail();
	 BigDecimal bigDecimal = new BigDecimal(0);
	 //获取需要扣除的积分值  及 用户id
	 Object integral = needClearIntegral.get("integral");
	 Object userId = needClearIntegral.get("userId");
	 //获取用户积分值
	 List<Integer> list = new ArrayList<Integer>(3);
	 UserInfoVo userInfoVo = new UserInfoVo();
	 list.add(1);
	 list.add(2);
	 AccountInfoDto accountInfoDto = remoteMiniAccountService.accountInfo(userId.toString(), list);
	 System.out.println(accountInfoDto);
	 BigDecimal userNowIntegral = accountInfoDto.getMiniAccountExtends().getIntegral();
	 //扣除规则
	 /**
	 if(用户现有积分小于等于本次清空的积分)
	 生成清空明细 现有积分值为0.00 (最低是0)
	 积分明细状态为true(已清空)
	 (因为最低是0 所以下次进行计算时本次扣除积分应不再做计算(不然会出现现有积分值小于0的情况))
	 修改用户这个商铺的所有积分明细为清空状态
	 用户扣除积分为 用户当前所有积分(也就是用户积分获取)
	 else(用户现有积分大于本次清空的积分)
	 生成清空明细 现有积分值为  (现有积分值减去本次清空的积分)
	 积分明细状态为false(为清空 ：因下次需要计算)
	 修改用户这个商铺本次需要清空的积分明细为清空状态
	 用户扣除积分为 integral(用户本次需要清空的积分总和)
	 */
	/**
	 //用户现有积分小于等于本次需要扣除的积分
	 if (userNowIntegral.compareTo((BigDecimal) integral) < 1) {
	 //生成积分明细
	 integralDetail.setIntegral(((BigDecimal) integral).negate());
	 integralDetail.setUserId(Long.valueOf(userId.toString()));
	 integralDetail.setShopId(shopId);
	 integralDetail.setTenantId(tenantId);
	 integralDetail.setNowTheIntegral(bigDecimal);
	 integralDetail.setGainIntegralType(IntegralGainTypeEnum.EMPTY.getStatus());
	 integralDetail.setClear(true);
	 int insert = integralDetailMapper.insert(integralDetail);
	 if (insert < 1) {
	 throw new ServiceException("积分清空明细插入失败", SystemCode.DATA_ADD_FAILED.getCode());
	 }
	 //通知用戶
	 IntegraChangelDto integraChangelDto = new IntegraChangelDto();
	 integraChangelDto.setValue(bigDecimal);
	 integraChangelDto.setUserId(userId.toString());
	 integraChangelDto.setOption(IntegralOperationTypeEnum.REDUCE.getStatus());
	 integraChangelDto.setTenantId(tenantId);
	 sender.sendIntegralDeductionMessage(integraChangelDto);
	 } else {
	 integralDetail.setIntegral(((BigDecimal) integral).negate());
	 integralDetail.setUserId(Long.valueOf(userId.toString()));
	 integralDetail.setShopId(shopId);
	 integralDetail.setTenantId(tenantId);
	 integralDetail.setGainIntegralType(IntegralGainTypeEnum.EMPTY.getStatus());
	 bigDecimal = integralDetailMapper
	 .selectNowIntegral(integralDetail.getShopId(), Long.valueOf(userId.toString()));
	 integralDetail.setNowTheIntegral(bigDecimal.add(((BigDecimal) integral).negate()));
	 integralDetail.setClear(false);
	 int insert = integralDetailMapper.insert(integralDetail);
	 if (insert < 1) {
	 throw new ServiceException("积分清空明细插入失败", SystemCode.DATA_ADD_FAILED.getCode());
	 }
	 //通知用戶
	 IntegraChangelDto integraChangelDto = new IntegraChangelDto();
	 integraChangelDto.setValue((BigDecimal) integral);
	 integraChangelDto.setUserId(userId.toString());
	 integraChangelDto.setOption(IntegralOperationTypeEnum.REDUCE.getStatus());
	 integraChangelDto.setTenantId(tenantId);
	 sender.sendIntegralDeductionMessage(integraChangelDto);
	 }
	 }
	 */
}
